const express = require('express');
const cors = require('cors');
const contorller = require('./controllers/dbController');
const mockContoller = require('./controllers/mockController');
const formHandle = require('./controllers/requestController');
const app = express();
// var fs = require('fs');
// 跨域处理
app.use(cors());

// 系统路由
app.get('/graduation-project/login', (req, res, next) => {
  res.sendFile(__dirname + '/public/graduation-project/login.html');
});

app.get('/graduation-project/registered', (req, res, next) => {
  res.sendFile(__dirname + '/public/graduation-project/registered.html');
});

// 登录页表单post提交
app.post('/graduation-project/login-handle', formHandle.loginHandle);

app.post('/graduation-project/registered-handle', formHandle.registeredHandle);

// 获取教室环境数据
app.get('/api/class-data', (req, res, next) => {
  const data = mockContoller.getClassData();
  res.json(data);
});

app.get('/api/machine-data', (req, res, next) => {
  contorller.getMachineData(result => {
    res.json(result);
  });
});

// 获取设备一数据
app.get('/api/machine-data/:index', (req, res, next) => {
  contorller.getOneMachine(req.params.index, result => {
    res.json(result);
  });
});

// 静态化api文件夹
app.use(express.static('public'));

app.listen(8080, () => {
  console.log('服务器正在监听');
});

// F:\ComputerScience\WebDeveloping\graduation_project\server\public\graduation-project
// 'F:\ComputerScience\WebDeveloping\graduation_project\server.\public\graduation-project\login.html'
